java如何将对象从servlet传递到JSP?
我可以通过Integer
、String
、Float
等等。。但是当我传递我定义的对象(Employee
)时,JSP将它作为null
接收
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="com.rahul.model.bean.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Search Result</title>
</head>
<body>
<%
Employee record = (Employee) request.getAttribute("searchResult");
out.println(record);
%>
<table border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Designation</th>
<th>Department</th>
<th>Salary</th>
</tr>
</table>
</body>
</html>
And My Controlleer doGet is:
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
EmployeeDAO dao = new EmployeeDAOImpl();
Employee result = dao.search(request.getParameter("id"));
// PrintWriter pw=response.getWriter();
// pw.println(result);
ServletContext app = getServletContext();
app.setAttribute("searchResult", result);
System.out.println("Emp= "+result);
response.sendRedirect("./searchview.jsp");
}
# 1 楼答案
试试这个:
欢迎小轿车。爪哇
你好。jsp
这不适用于
sendRedirect
,因为您基本上是在客户机和服务器之间进行往返,跨越2个请求,而不是一个请求。第一个请求有您的参数,但是由于您的客户端没有存储它,因此在重定向发生时它将丢失。您应该转发到JSP,除非servlet所做的事情不应该反复执行(比如数据库插入)。如果您真的需要重定向,请查看here